package com.javafly.sms.web.security;

import java.util.List;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.GrantedAuthority;

import com.javafly.sms.entity.security.Authority;
import com.javafly.sms.entity.security.Role;
import com.javafly.sms.service.security.SecurityEntityManager;
import com.javafly.sms.web.CrudActionSupport;
import org.springside.modules.orm.hibernate.HibernateWebUtils;
import org.springside.modules.security.springsecurity.SpringSecurityUtils;
import org.springside.modules.web.struts2.Struts2Utils;

/**
 * 菜单查询Action.
 * 
 * 演示不分页的简单管理界面.
 * 
 * @author hym
 */
@Namespace("/security")
@Results( { @Result(name = CrudActionSupport.RELOAD, location = "/login.action", type = "redirect") })
public class MenuAction extends CrudActionSupport<com.javafly.sms.entity.security.Resource> {

	private static final long serialVersionUID = 1L;

	@Autowired
	private SecurityEntityManager securityEntityManager;
	
	//-- 页面属性 --//
	private Long id;
	private  com.javafly.sms.entity.security.Resource entity;
	private List< com.javafly.sms.entity.security.Resource> myResourceList;//权限列表
	
	public List<com.javafly.sms.entity.security.Resource> getMyResourceList() {
		return myResourceList;
	}





	private String str;
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getStr() {
		str="haha 哈哈";
		return str;
	}
     
	public void setStr(String str) {
		this.str = str;
	}

	//-- ModelDriven 与 Preparable函数 --//
	public  com.javafly.sms.entity.security.Resource getModel() {
		return entity;
	}
	@Override
	protected void prepareModel() throws Exception {
		if (id != null) {
			entity = securityEntityManager.getResource(id);
		} else {
			entity = new  com.javafly.sms.entity.security.Resource();
		}
	}

	//-- CRUD Action 函数 --//
	@Override
	public String list() throws Exception {
		if(id==null){
			id=-1L;
		}
		myResourceList = securityEntityManager.getSubResource(id);
		//去除自己没有权限的 菜单
		for(int i=0;i<myResourceList.size();i++){
			com.javafly.sms.entity.security.Resource tmpr=myResourceList.get(i);
			GrantedAuthority[] gas=SpringSecurityUtils.getCurrentUser().getAuthorities();
			boolean flag= false;
			for(int k=0;k<gas.length;k++){
				String authname=gas[k].getAuthority();
				if(tmpr.getAuthNames().indexOf(authname)!=-1){
					flag=true;//自己有这个权限。
					break;
				}
			}
			if(!flag){
				myResourceList.remove(i);
				i--;
			}
			
		}
		return SUCCESS;
	}
	@Override
	public String input() throws Exception {
		
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		
		return RELOAD;
	}
	
	
	public String mmain()throws Exception {
		if(SpringSecurityUtils.getCurrentUserName().equals("roleAnonymous")){
			return RELOAD;
		}
		return "mmain";//到menu-mmain.jsp 页面
	}
	public String right()throws Exception {
		
		return "right";//到menu-mmain.jsp 页面
	}
	public String bottom()throws Exception {
		
		return "bottom";//到menu-mmain.jsp 页面
	}
	public String top()throws Exception {
		
		id=-1L;
		myResourceList = securityEntityManager.getSubResource(id);
		//去除自己没有权限的 菜单
		try{
		for(int i=0;i<myResourceList.size();i++){
			com.javafly.sms.entity.security.Resource tmpr=myResourceList.get(i);
			GrantedAuthority[] gas=SpringSecurityUtils.getCurrentUser().getAuthorities();
			boolean flag= false;
			for(int k=0;k<gas.length;k++){
				String authname=gas[k].getAuthority();
				if(tmpr.getAuthNames().indexOf(authname)!=-1){
					flag=true;//自己有这个权限。
					break;
				}
			}
			if(!flag){
				myResourceList.remove(i);
				i--;
			}
			
		}
		}catch(Exception e){
			return RELOAD;
		}
		return "top";//到menu-top.jsp 页面
	}
	public String left()throws Exception {
		if(SpringSecurityUtils.getCurrentUserName().equals("roleAnonymous")){
			return "lefttop";
		}
		if(id==null){
			return "left";
		}
		myResourceList = securityEntityManager.getSubResource(id);
		//去除自己没有权限的 菜单
		for(int i=0;i<myResourceList.size();i++){
			com.javafly.sms.entity.security.Resource tmpr=myResourceList.get(i);
			GrantedAuthority[] gas=SpringSecurityUtils.getCurrentUser().getAuthorities();
			boolean flag= false;
			for(int k=0;k<gas.length;k++){
				String authname=gas[k].getAuthority();
				if(tmpr.getAuthNames().indexOf(authname)!=-1){
					flag=true;//自己有这个权限。
					break;
				}
			}
			if(!flag){
				myResourceList.remove(i);
				i--;
			}
			
		}
		return "left";//到menu-left.jsp 页面
	}

    public String getMenuStr(){
    	String menustr="<ul class=\"tree\" style=\"margin-left: 15px;\">";
    	
    	menustr=menustr+"</ul>";
    	return menustr;
    }
   
    
    
    
    
    
	
}